import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login"
import Resgister from '../views/Register'
import Layout from "../views/Layout"
import Book from "../views/Book"
import Blog from '../views/Blog'
import Video from "../views/Video"
import Resource from "../views/Resource"
import User from "../views/User"
import CreateBlog from "../components/CreateBlog"
import Chapter from "../components/Chapter"
import Section from "../components/Section"
import CreateSection from "../components/CreateSection"
Vue.use(VueRouter)

const routes = [{
        path: "/",
        redirect: "/book"
    },
    {
        path: '/login',
        name: "Login",
        component: Login,
    },
    {
        path: '/register',
        name: "Register",
        component: Resgister
    },
    {
        path: "/layout",
        name: "Layout",
        component: Layout,
        children: [{
                path: "/book",
                name: "book",
                component: Book
            },
            {
                path: "/blog",
                name: "blog",
                component: Blog
            },
            {
                path: "/createblog",
                name: "createblog",
                component: CreateBlog,
                props($route){
                    return {id:$route.query.id}
            }
            },
            {
                path: "/video",
                name: "video",
                component: Video
            },
            {
                path: "/resource",
                name: "resource",
                component: Resource
            },
            {
                path: "/user",
                name: "user",
                component: User
            },
            {
                path: "/chapter",
                name: "chapter",
                component: Chapter
            },
            {
                path: "/section",
                name: "section",
                component: Section
            },
            {
                path: "/createsection",
                name: "createsection",
                component: CreateSection
            }
        ]
    }
]


const router = new VueRouter({
    // mode: 'history',   //模式  history hash ，
    base: process.env.BASE_URL, //定义一个基础URL，用的是vue-cli默认的配置项。
    routes //配置项
})
router.beforeEach((to, from, next) => {
    if (to.path === "/login" || to.path === "/register") {
        next()
    } else {
        if (localStorage.getItem("token") || sessionStorage.getItem("token")) {
            next()
        } else {
            next("/login")
        }
    }
})

export default router